﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using ESOL.Common.Entity;
using ESOL.Business.Inv;
using ESOL.Business.Mst;
using ESOL.Common;

namespace ESOL.WinUI.Inv
{
    public partial class frmManageStorageChange : BaseForm.frmBaseMdiBusinessForm
    {

        #region Variable
        private InvDivertOrderEntity mDivertOrder;
        #endregion

        #region Constructor
        public frmManageStorageChange()
        {
            InitializeComponent();
        }
        #endregion

        #region Functions
        private void BindComboBox()
        {
            UICommon.UICommonFunctions.BindComboboxByCommonItem(cmbStatus, "ICO", false);
            UICommon.UICommonFunctions.BindGridComboboxByCommonItem(repositoryItemGridLookUpEdit1, "ICO", false);

            List<MstWarehouseEntity> list = MstWarehouseBs.GetMstWarehouseAllList();
            MstWarehouseEntity entity = new MstWarehouseEntity();
            list.Insert(0, entity);
            cmbWareHouseCode.DataSource = list;
            cmbWareHouseCode.DisplayMember = "WarehouseName";
            cmbWareHouseCode.ValueMember = "WarehouseCode";
        }
        private string GetCondition()
        {
            string condition = "";
            if (!string.IsNullOrEmpty(txtOrderCode.Text))
            {
                condition += "AND A.ORDER_CODE LIKE '%" + txtOrderCode.Text + "%'";
            }
            if (!string.IsNullOrEmpty(txtOrderPerson.Text))
            {
                MstPersonEntity entity = (MstPersonEntity)txtOrderPerson.Tag;
                condition += " AND A.CREATED_BY = '" + entity.PersonId + "' ";
            }
            if (!string.IsNullOrEmpty(txtRelatedOrder.Text))
            {
                condition += " AND A.RELATED_ORDER LIKE '%" + txtRelatedOrder.Text + "%'";
            }
            if (!string.IsNullOrEmpty(cmbWareHouseCode.Text))
            {
                condition += " AND B.WAREHOUSE_CODE = '" + cmbWareHouseCode.SelectedValue.ToString() + "'";
            }
            if (!string.IsNullOrEmpty(cmbOrderType.Text))
            {
                condition += " AND A.ORDER_TYPE = '" + cmbOrderType.SelectedValue.ToString() + "'";
            }
            if (!string.IsNullOrEmpty(cmbStatus.Text))
            {
                condition += " AND A.STATUS = '" + cmbStatus.SelectedValue.ToString() + "'";
            }
            if (dtpOrderDate.EditValue!=null)
            {
                condition += " AND A.ORDER_DATE >= CONVERT(DATETIME,'" + dtpOrderDate.EditValue + "')";
            }
            if (dtpOrderDateTo.EditValue != null)
            {
                condition += " AND A.ORDER_DATE <= CONVERT(DATETIME,'" + dtpOrderDateTo.EditValue + "')";
            }
            return condition;
        }
        private void ResetButtonEnable()
        {
            btnView.Enabled = true;
            btnNew.Enabled = true;
            btnModify.Enabled = true;
            btnApprove.Enabled = true;
            btnDelete.Enabled = true;
            if(mDivertOrder==null)
            {
                return;
            }
            if (mDivertOrder.Status == "ICO03"||mDivertOrder.Status=="ICO05")
            {
                btnModify.Enabled = false;
                btnApprove.Enabled = false;
                btnDelete.Enabled = false;
            }

        }
        private void BindDataGrid()
        {
             string condition = GetCondition();
            string rowCount = txtRowCount.Text;
            string orderBy = "A.CREATED_DATE DESC";
            DataTable dt = InvDivertOrderBs.GetByCondition(condition, orderBy, rowCount);
            gvMain.DataSource = dt;
        }
        #endregion

        private void frmManageStorageChange_Load(object sender, EventArgs e)
        {
            BindComboBox();
            BindDataGrid();
        }

        protected override void Click_Query()
        {
            BindDataGrid();
        }
        protected override void Click_New()
        {
            frmMaintainStorageChange frm = new frmMaintainStorageChange();
            frm.ThisActionType = ActionType.New;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        protected override void Click_Modify()
        {
            if (mDivertOrder == null)
            {
                MessageBox.Show("请选择一个移库单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainStorageChange frm = new frmMaintainStorageChange();
            frm.selectedDivertOrder = mDivertOrder;
            frm.ThisActionType = ActionType.Modify;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        protected override void Click_Delete()
        {
            if (mDivertOrder == null)
            {
                MessageBox.Show("请选择一个移库单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            DialogResult dr = MessageBox.Show("是否确认删除选中移库单", "Question", MessageBoxButtons.YesNo, MessageBoxIcon.Question);
            if (dr == DialogResult.No)
            {
                return;
            }
            try
            {
                mDivertOrder.Status = "ICO05";
                mDivertOrder.LastModifiedBy = CommonParameter.CurrentUser.PersonId;
                mDivertOrder.LastModifiedDate = DateTime.Now;
                bool successful = InvDivertOrderBs.UpdateInvDivertOrder(mDivertOrder);
                if (successful)
                {
                    MessageBox.Show("删除成功！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                    BindDataGrid();
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message, "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
            }
        }
        protected override void Click_Approve()
        {
            if (mDivertOrder == null)
            {
                MessageBox.Show("请选择一个入库申请单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }

            frmMaintainStorageChange frm = new frmMaintainStorageChange();
            frm.ThisActionType = ActionType.Approve;
            frm.selectedDivertOrder = mDivertOrder;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }
        protected override void Click_View()
        {
            if (mDivertOrder == null)
            {
                MessageBox.Show("请选择一个移库单！", "Information", MessageBoxButtons.OK, MessageBoxIcon.Information);
                return;
            }
            frmMaintainStorageChange frm = new frmMaintainStorageChange();
            frm.selectedDivertOrder = mDivertOrder;
            frm.ThisActionType = ActionType.View;
            DialogResult dr = frm.ShowDialog();
            if (dr == DialogResult.OK)
            {
                BindDataGrid();
            }
        }

        private void gridView1_RowClick(object sender, DevExpress.XtraGrid.Views.Grid.RowClickEventArgs e)
        {
            DataRow dr=gridView1.GetDataRow(e.RowHandle);
            mDivertOrder = new InvDivertOrderEntity(dr);
            ResetButtonEnable();
        }

    }
}
